package com.fashopcosmos.preferencias.mapreduce;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import com.fashopcosmos.preferencias.beans.PreferenceCustomerBean;

public class PreferenceMap extends MapReduceBase
implements Mapper<LongWritable, Text, Text, PreferenceCustomerBean >{
	private Text  tIdEmpresa				  = new Text();
	private PreferenceCustomerBean preference = new PreferenceCustomerBean();
	private String 	idEmpresa;
	private int 	idLocal;
	private int 	edad;
	private String	sexo;
	private String	producto;
	
	@Override
	public void map(LongWritable key, Text value,
					OutputCollector<Text, PreferenceCustomerBean> output,
					Reporter reporter)
	throws IOException {
		String sLine				= value.toString();
		StringTokenizer	tokenizer 	= new StringTokenizer(sLine,",");
		//datos de fichero
		idEmpresa	= tokenizer.nextToken().trim();
		idLocal 	= Integer.valueOf(tokenizer.nextToken().trim());
		edad		= Integer.valueOf(tokenizer.nextToken().trim());
		sexo 	    = tokenizer.nextToken().trim();
		producto	= tokenizer.nextToken().trim();
		
		//asignamos el id al Text
		tIdEmpresa.set(idEmpresa);		
		//rellenamos los datos del beans
		preference.setIdLocal(idLocal);
		preference.setProducto(producto);
		preference.setEdad(edad);
		preference.setSexo(sexo);
		//
		output.collect(this.tIdEmpresa, this.preference);
		
	}
}
